package com.itc.bi.utils;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: itc-airport-cloud
 * @BelongsPackage: com.itc.bi.utils
 * @Author: hecaiy
 * @CreateTime: 2024-07-23  14:03
 * @Description: TODO EQ(=), NE(!=), GE(>=), GT(>), LE(<=), LT(<), IN(in), NIN(not in),LIKE(like)
 * @Version: 1.0
 */
public class APIQueryUtil {
    public static final String EQ = " = ";
    public static final String NE = " != ";
    public static final String GE = " >= ";
    public static final String GT = " > ";
    public static final String LE = " <= ";
    public static final String LT = " < ";
    public static final String IN = " in ";
    public static final String NIN = " not in ";
    public static final String LIKE = " like ";

    /**
     * 凭借sql
     *
     * @param key
     * @param data
     * @return
     */
    public static String querySQLStr(String key, Object data) {
        String sqlStr = " ";
        // =
        if (key.startsWith("EQ_")) {
            sqlStr = key.replace("EQ_","") + EQ + " '" + data.toString() + "'";
        }
        //!=
        if (key.startsWith("NE_")) {
            sqlStr = key.replace("NE_","") + NE + " '" + data.toString() + "'";
        }
        //GE = ">=";
        if (key.startsWith("GE_")) {
            sqlStr = key.replace("GE_","") + GE + " '" + data.toString() + "'";
        }
        //GT = ">"
        if (key.startsWith("GT_")) {
            sqlStr = key.replace("GT_","") + GT + " '" + data.toString() + "'";
        }
        //LE = "<="
        if (key.startsWith("LE_")) {
            sqlStr = key.replace("LE_","") + LE + " '" + data.toString() + "'";
        }
        //LT = "<";
        if (key.startsWith("LT_")) {
            sqlStr = key.replace("LT_","") + LT + " '" + data.toString() + "'";
        }
        //IN = "in";
        if (key.startsWith("IN_")) {
            String pinjie = "";
            if(data instanceof ArrayList){
                List<String> objects = JSON.parseArray(JSON.toJSONString(data),String.class);
                for (int i = 0; i < objects.size(); i++) {
                    pinjie=pinjie+"'" + objects.get(i) + "',";
                }
            }
            pinjie = pinjie.substring(0,pinjie.length()-1);
            sqlStr = key.replace("IN_","") + IN + "(" + pinjie + ")";
        }
        //NIN = "not in";
        if (key.startsWith("NIN_")) {
            String pinjie = "";
            if(data instanceof ArrayList){
                List<String> objects = JSON.parseArray(JSON.toJSONString(data),String.class);
                for (int i = 0; i < objects.size(); i++) {
                    pinjie=pinjie+"'" + objects.get(i) + "',";
                }
            }
            pinjie = pinjie.substring(0,pinjie.length()-1);
            sqlStr = key.replace("NIN_","") + NIN + "(" + pinjie + ")";
        }
        //LIKE = "like";
        if (key.startsWith("LIKE_")) {
            sqlStr = key.replace("LIKE_","") + LIKE + " '%" + data.toString() + "%'";
        }
        return sqlStr;
    }
}
